package com.ssfeng.youxia.controller;

import com.ssfeng.youxia.services.EnvironmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
import utils.FileDowloadUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Handles requests for the application file upload requests
 */
@Controller
@RequestMapping(value = "/file")
public class FileController {

    private static final Logger logger = LoggerFactory.getLogger(FileController.class);

    @Autowired
    private MappingJackson2JsonView mappingJackson2JsonView;

    @Autowired
    private EnvironmentService environmentService;

    /***
     * 一个文件上传的demo
     * @param names
     * @param files
     * @return
     */
    @RequestMapping(value = "/upload")
    public ModelAndView uploadMultipleFileHandler(@RequestParam("name") String[] names,
                                                  @RequestParam("file") MultipartFile[] files) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setView(mappingJackson2JsonView);
        Map<String, String> map = new HashMap<>();
        if (files.length != names.length){
            map.put("code", "-1");
            map.put("msg", "Mandatory information missing");
            modelAndView.addObject("data", map);
            return modelAndView;
        }
        String message = "";
        for (int i = 0; i < files.length; i++) {
            MultipartFile file = files[i];
            String name = names[i];
            try {
                byte[] bytes = file.getBytes();
                String basepath = environmentService.getProperty("file.save.dir");
                File dir = new File(basepath );
                if (!dir.exists())
                    dir.mkdirs();
                File serverFile = new File(dir.getAbsolutePath()
                        + File.separator + name);
                BufferedOutputStream stream = new BufferedOutputStream(
                        new FileOutputStream(serverFile));
                stream.write(bytes);
                stream.close();
            } catch (Exception e) {
                logger.error("文件上传异常",e);
            }
        }
        modelAndView.addObject("data", map);
        return modelAndView;
    }

    /***
     * 文件下载demo
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/dowload")
    @ResponseBody
    public void dowload(HttpServletResponse response) throws IOException {
        File file = new File("/Users/chanming/Desktop/a.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        FileDowloadUtils.dowloadFile(response,fileInputStream,"a.txt");
    }

    @RequestMapping(value = "/test")
    public void test(){
        System.out.println("test");
    }


}